using System;
using System.Collections;
using System.Data;
using BeeldBank.Business;
using BeeldBank.Business.Collection;

namespace BeeldBank.Business.Base
{
	/// <summary>
	/// Provides database independant methods for Status.
	/// </summary>
	public class StatusBase : BusinessBase
	{
		internal StatusBase(System.Data.DataRow row) : base(row)
		{
			this.OnPrepareForSave += new PrepareForSaveHandler(this.PrepareForSave);
		}

		public delegate void PrepareForSaveHandler();
		public event PrepareForSaveHandler OnPrepareForSave;

		#region property bool EnforceSave
		/// This property is used to indicate if the
		/// object in the save event should save, even
		/// if the object contents (marked as stereotype NOTENFORCESAVE) is not saved
		private bool _EnforceSave=false;
		public bool EnforceSave
		{
			get
			{
				return _EnforceSave;
			}
			set
			{
				_EnforceSave = value;
			}
		
		}
		#endregion



		#region PrepareForSaveBase()
		public void PrepareForSaveBase()
		{
			if (!this.PrepareForSaveFinished)
			{
				if (OnPrepareForSave != null)
				{
					OnPrepareForSave();
				}
				this.CanLoad = false;
				// call prepareforsave on all dependant objects.
			
				this.CanLoad = true;
				this.PrepareForSaveFinished = true;
			}
		}
		#endregion

		#region PrepareForSave()
		public virtual void PrepareForSave()
		{
		}
		#endregion

		#region PreSave()
		public virtual void PreSave()
		{
		}
		#endregion
				
		#region Save()
		public void Save()
		{
			bool IsSaveNeeded=ContentChanged();
			
			if (IsDirty)
			{
				this.ModifiedBy = _luw.UserName;
				if (this.State == BusinessState.New)
				{
					this.CreatedBy = _luw.UserName;
					BeeldBank.Data.Status.Insert(this._dataRow, _luw.Transaction);
					
				}
			
				if ( ( this.State == BusinessState.Changed ) && ( IsSaveNeeded ) )
				{
					BeeldBank.Data.Status.Update(this._dataRow, _luw.Transaction);
				}
				
				if ( this.State == BusinessState.Deleted )
				{
					BeeldBank.Data.Status.Delete(this._dataRow, _luw.Transaction);
					
				}
				IsDirty = false;
			}
		}
		#endregion
		
		#region OldStatus
		private Status _oldStatus;
		public Status OldStatus
		{
			get
			{
				if (_oldStatus == null && this.State != BusinessState.New)
				{
					// Create the new object in a completely separate luw,
					// otherwise a reference the the current object is returned.
					Luw luw = new Luw(this.Luw.UserName);
					_oldStatus = luw.NewStatus(BeeldBank.Data.Status.Open(this.Id).Rows[0]);
				}
				return _oldStatus;
			}
		}
		#endregion
		
		#region Id
		/// <summary>
		/// Primaire sleutel
		/// </summary>
		public int Id
		{
			get
			{
				return Convert.ToInt32(_dataRow["Id"]);
			}
			set
			{
				_dataRow["Id"] = value;
				this.IsDirty = true;
			}
		}
		public bool IdIsNull
		{
			get
			{
				return _dataRow["Id"] == Convert.DBNull;
			}
		}
		public void IdSetNull()
		{
			_dataRow["Id"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Name
		/// <summary>
		/// Naam van de status
		/// </summary>
		public string Name
		{
			get
			{
				return Convert.ToString(_dataRow["Name"]);
			}
			set
			{
				_dataRow["Name"] = value;
				this.IsDirty = true;
			}
		}
		public bool NameIsNull
		{
			get
			{
				return _dataRow["Name"] == Convert.DBNull;
			}
		}
		public void NameSetNull()
		{
			_dataRow["Name"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region Visible
		/// <summary>
		/// Is een record met deze status zichtbaar
		/// </summary>
		public bool Visible
		{
			get
			{
				return (_dataRow["Visible"] == Convert.DBNull) ? false : Convert.ToBoolean(_dataRow["Visible"]);
			}
			set
			{
				_dataRow["Visible"] = value;
				this.IsDirty = true;
			}
		}
		public bool VisibleIsNull
		{
			get
			{
				return _dataRow["Visible"] == Convert.DBNull;
			}
		}
		public void VisibleSetNull()
		{
			_dataRow["Visible"] = Convert.DBNull;
			this.IsDirty = true;
		}
		#endregion
			
		#region CreatedBy
		public String CreatedBy
		{
			get
			{
				return Convert.ToString(_dataRow["CreatedBy"]);
			}
			set
			{
				_dataRow["CreatedBy"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region CreatedAt
		public DateTime CreatedAt
		{
			get
			{
				return Convert.ToDateTime(_dataRow["CreatedAt"]);
			}
			set
			{
				_dataRow["CreatedAt"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region ModifiedBy
		public String ModifiedBy
		{
			get
			{
				return Convert.ToString(_dataRow["ModifiedBy"]);
			}
			set
			{
				_dataRow["ModifiedBy"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region ModifiedAt
		public DateTime ModifiedAt
		{
			get
			{
				return Convert.ToDateTime(_dataRow["ModifiedAt"]);
			}
			set
			{
				_dataRow["ModifiedAt"] = value;
				this.IsDirty = true;
			}
		}
		#endregion
	
		#region Version
		public int Version
		{
			get
			{
				return _dataRow["Version"] == Convert.DBNull ? -1 : Convert.ToInt32(_dataRow["Version"]);
			}
			set
			{
				_dataRow["Version"] = value;
				this.IsDirty = true;
			}
		}
		#endregion

		#region MediaObject
		private MediaObjectCollection _mediaObject;
		public MediaObjectCollection MediaObject
		{
			get
			{
				if (_mediaObject == null && _luw.CanLoad && this.CanLoad && !IdIsNull)
				{
					_mediaObject = new MediaObjectCollection(_luw, BeeldBank.Data.MediaObject.OpenFromStatusId(Id));
				}
				if (_mediaObject == null && _luw.CanLoad && this.CanLoad && IdIsNull)
				{
					_mediaObject = new MediaObjectCollection(_luw);
				}
				return _mediaObject;
			}				
		}
		#endregion
		

		#region ContentChanged
		/// <summary>
		/// ContentChanged
		/// This function checks whether the content of a record is changed, dependant on the "EnforceSave" 
		/// variable. If EnforceSave is false, all the fields which have not been marked "NOTENFORCINGSAVE" as
		/// a stereotype in the design are checked. The contents of the fields are checked against the old values
		/// if they are not equal,the result is true. If the contents are equal, the result is false.
		/// 
		/// If EnforceSave is true, the result is always true.
		/// </summary>
		/// <returns>boolean indicating whether the Content is Changed</returns>
		public bool ContentChanged()
		{
			// Change enforced by Class Variables
			if( this.EnforceSave ) { return( true ); }
			if( this.IdIsNull) { return( true ); }
			
			// Change enforced by the attributes
			
			#region Id
			/// Id is excluded because it is the primary key
			#endregion
					
			#region Name
			
			if( this.NameIsNull != this.OldStatus.NameIsNull ) { return( true ); }
			if( this.Name != this.OldStatus.Name) { return( true ); }
	
			#endregion
					
			#region Visible
			
			if( this.VisibleIsNull != this.OldStatus.VisibleIsNull ) { return( true ); }
			if( this.Visible != this.OldStatus.Visible) { return( true ); }
	
			#endregion
							
			
			return false;
		}
		#endregion	

		
		#region WhereAmI
		/// <summary>
		/// Provides the WhereAmI display text for a Status with the specified id
		/// </summary>
		/// <param name="id">Id of the Status.</param>
		/// <return>>WhereAmI display text</returns>
		public static string WhereAmI(int id)
		{
			string whereAmI = "";
			if (id == 0)
			{
				whereAmI = "New Status";
			}
			else
			{
				DataTable table = BeeldBank.Data.Status.Show(id);
				
				whereAmI = whereAmI + table.Rows[0]["Name"].ToString();
				
			}
			return whereAmI;
		}
		#endregion
		
	}
	

	
	
}
